package com.vehiclesecure.controller;

import com.vehiclesecure.model.MaintenanceRecord;
import com.vehiclesecure.service.MaintenanceRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/maintenance")
@CrossOrigin(origins = "http://localhost:3002")
public class MaintenanceRecordController {

    @Autowired
    private MaintenanceRecordService maintenanceRecordService;

    @PostMapping
    @PreAuthorize("hasRole('USER') or hasRole('ADMIN')")
    public ResponseEntity<MaintenanceRecord> createMaintenanceRecord(
            @RequestBody MaintenanceRecord record,
            Authentication authentication) {
        try {
            MaintenanceRecord createdRecord = maintenanceRecordService.createMaintenanceRecord(record, authentication);
            return ResponseEntity.ok(createdRecord);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(null);
        }
    }

    @GetMapping("/vehicle/{vehicleId}")
    @PreAuthorize("hasRole('USER') or hasRole('ADMIN') or hasRole('SERVICE_PROVIDER')")
    public ResponseEntity<List<MaintenanceRecord>> getMaintenanceRecordsByVehicle(
            @PathVariable String vehicleId,
            Authentication authentication) {
        try {
            List<MaintenanceRecord> records = maintenanceRecordService.getMaintenanceRecordsByVehicle(vehicleId, authentication);
            return ResponseEntity.ok(records);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(null);
        }
    }

    @GetMapping("/service-provider")
    @PreAuthorize("hasRole('SERVICE_PROVIDER')")
    public ResponseEntity<List<MaintenanceRecord>> getMaintenanceRecordsForServiceProvider(Authentication authentication) {
        try {
            List<MaintenanceRecord> records = maintenanceRecordService.getMaintenanceRecordsForServiceProvider(authentication);
            return ResponseEntity.ok(records);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(null);
        }
    }

    @GetMapping("/service-provider/pending")
    @PreAuthorize("hasRole('SERVICE_PROVIDER')")
    public ResponseEntity<List<MaintenanceRecord>> getPendingMaintenanceRecordsForServiceProvider(Authentication authentication) {
        try {
            List<MaintenanceRecord> records = maintenanceRecordService.getPendingMaintenanceRecordsForServiceProvider(authentication);
            return ResponseEntity.ok(records);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(null);
        }
    }

    @GetMapping("/my-reports")
    @PreAuthorize("hasRole('USER') or hasRole('ADMIN')")
    public ResponseEntity<List<MaintenanceRecord>> getMyReportedRecords(Authentication authentication) {
        try {
            List<MaintenanceRecord> records = maintenanceRecordService.getMyReportedRecords(authentication);
            return ResponseEntity.ok(records);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(null);
        }
    }

    @GetMapping("/{recordId}")
    @PreAuthorize("hasRole('USER') or hasRole('ADMIN') or hasRole('SERVICE_PROVIDER')")
    public ResponseEntity<MaintenanceRecord> getMaintenanceRecord(
            @PathVariable String recordId,
            Authentication authentication) {
        try {
            MaintenanceRecord record = maintenanceRecordService.getMaintenanceRecord(recordId, authentication);
            return ResponseEntity.ok(record);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(null);
        }
    }

    @PutMapping("/{recordId}/assign/{serviceProviderId}")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<MaintenanceRecord> assignToServiceProvider(
            @PathVariable String recordId,
            @PathVariable String serviceProviderId,
            Authentication authentication) {
        try {
            MaintenanceRecord record = maintenanceRecordService.assignToServiceProvider(recordId, serviceProviderId, authentication);
            return ResponseEntity.ok(record);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(null);
        }
    }

    @PutMapping("/{recordId}/status")
    @PreAuthorize("hasRole('SERVICE_PROVIDER')")
    public ResponseEntity<MaintenanceRecord> updateMaintenanceStatus(
            @PathVariable String recordId,
            @RequestParam MaintenanceRecord.MaintenanceStatus status,
            @RequestParam(required = false) String notes,
            Authentication authentication) {
        try {
            MaintenanceRecord record = maintenanceRecordService.updateMaintenanceStatus(recordId, status, notes, authentication);
            return ResponseEntity.ok(record);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(null);
        }
    }

    @GetMapping("/service-provider/pending-count")
    @PreAuthorize("hasRole('SERVICE_PROVIDER')")
    public ResponseEntity<Long> getPendingCountForServiceProvider(Authentication authentication) {
        try {
            long count = maintenanceRecordService.getPendingCountForServiceProvider(authentication);
            return ResponseEntity.ok(count);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(0L);
        }
    }
}